package com.gs.wechatdemo.db.sqlite;

import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.Environment;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.gs.wechatdemo.constant.AllConstants;
import com.gs.wechatdemo.util.SPUtil;

public class SQLiteOpenHelper extends android.database.sqlite.SQLiteOpenHelper {

    private static final String CREATE_TABLE_START_SQL = "CREATE TABLE IF NOT EXISTS ";
    private static final String CREATE_INDEX_START_SQL = "CREATE INDEX IF NOT EXISTS ";
    private static final String CREATE_TABLE_PRIMARY_SQL = " INTEGER PRIMARY KEY AUTOINCREMENT,";

    public SQLiteOpenHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        createTables(db);
        createIndex(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (newVersion > oldVersion) {
            createIndex(db);
        }
    }

    private void createTables(SQLiteDatabase db) {
        StringBuilder sql = new StringBuilder();
        sql.append(CREATE_TABLE_START_SQL).append(SQLiteUtil.TABLE_CONTACT).append("(")
                .append("id").append(CREATE_TABLE_PRIMARY_SQL)
                .append("userOrGroupId VARCHAR(30),")
                .append("name TEXT,")
                .append("photo TEXT,")
                .append("date VARCHAR(30),")
                .append("content TEXT,")
                .append("level INTEGER,")
                .append("num INTEGER)");
        db.execSQL(sql.toString());
        sql = new StringBuilder();
        sql.append(CREATE_TABLE_START_SQL).append(SQLiteUtil.TABLE_CHAT).append("(")
                .append("id").append(CREATE_TABLE_PRIMARY_SQL)
                .append("chatId VARCHAR(30),")
                .append("groupId VARCHAR(30),")
                .append("userId VARCHAR(30),")
                .append("photo TEXT,")
                .append("content TEXT,")
                .append("date VARCHAR(30),")
                .append("photoW INTEGER,")
                .append("photoH INTEGER,")
                .append("viewType INTEGER)");
        db.execSQL(sql.toString());
    }

    private void createIndex(SQLiteDatabase db) {
        StringBuilder sql = new StringBuilder();
        sql.append(CREATE_INDEX_START_SQL).append(SQLiteUtil.INDEX_CONTACT)
                .append(" ON ")
                .append(SQLiteUtil.TABLE_CONTACT)
                .append(" (date)");
        db.execSQL(sql.toString());
        sql = new StringBuilder();
        sql.append(CREATE_INDEX_START_SQL).append(SQLiteUtil.INDEX_CONTACT_ID)
                .append(" ON ")
                .append(SQLiteUtil.TABLE_CONTACT)
                .append(" (userOrGroupId)");
        db.execSQL(sql.toString());
        sql = new StringBuilder();
        sql.append(CREATE_INDEX_START_SQL).append(SQLiteUtil.INDEX_CHAT)
                .append(" ON ")
                .append(SQLiteUtil.TABLE_CHAT)
                .append(" (id)");
        db.execSQL(sql.toString());
    }
}
